//
//  SetUnitsViewController.swift
//  RxSwift_Practice
//
//  Created by 李永彬 on 2020/5/18.
//  Copyright © 2020 李永彬. All rights reserved.
//

import UIKit

class SetUnitsViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .white
        // Do any additional setup after loading the view.
        let mileBtn = UIButton(type: .custom)
        mileBtn.addTarget(self, action: #selector(setMileUnitsForDevice), for: .touchUpInside)
        mileBtn.setTitle("设置英里", for: .normal)
        mileBtn.backgroundColor = UIColor.red
        mileBtn.titleLabel?.textColor = UIColor.white
        mileBtn.frame = CGRect(x: 150, y: 120, width: 100, height: 40)
        mileBtn.center.x = self.view.center.x
        mileBtn.sizeToFit()
        self.view.addSubview(mileBtn)
        
        let kmBtn = UIButton(type: .custom)
        kmBtn.addTarget(self, action: #selector(setKMUnitsForDevice), for: .touchUpInside)
        kmBtn.setTitle("设置千米", for: .normal)
        kmBtn.backgroundColor = UIColor.red
        kmBtn.titleLabel?.textColor = UIColor.white
        kmBtn.frame = CGRect(x: 150, y: 180, width: 100, height: 40)
        kmBtn.center.x = self.view.center.x
        kmBtn.sizeToFit()
        self.view.addSubview(kmBtn)
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .plain, target: self, action: #selector(back))
    }
    
    @objc func setKMUnitsForDevice() {
        UIApplication.shared.keyWindow!.makeToastActivity(.center)
        LYCentral.shared.setUnitsForDevice(unitType: 0) { (result, code) in
            UIApplication.shared.keyWindow!.hideToastActivity()
            UIApplication.shared.keyWindow!.hideToast()
            if result {
                print("设置千米成功")
                
                UIApplication.shared.keyWindow!.makeToast("设置千米成功", position: .center)
            } else {
                print("设置千米失败")
                UIApplication.shared.keyWindow!.makeToast("失败:\(String(describing: code!))", position: .center)
            }
        }
    }
    
    @objc func setMileUnitsForDevice() {
        UIApplication.shared.keyWindow!.makeToastActivity(.center)
        LYCentral.shared.setUnitsForDevice(unitType: 1) { (result, code) in
            UIApplication.shared.keyWindow!.hideToastActivity()
            UIApplication.shared.keyWindow!.hideToast()
            if result {
                print("设置英里成功")
                UIApplication.shared.keyWindow!.makeToast("设置英里成功", position: .center)
            } else {
                print("设置英里失败")
                UIApplication.shared.keyWindow!.makeToast("失败:\(String(describing: code!))", position: .center)
            }
        }
    }
    
    @objc func back() {

        self.navigationController?.setViewControllers([(self.navigationController?.viewControllers[0])!], animated: true)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
